package org.glut.cloud.materialManage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.glut.cloud.materialManage.entity.MaterialChange;
import org.glut.cloud.materialManage.entity.MaterialsManage;
import org.glut.cloud.materialManage.mapper.MaterialChangeMapper;
import org.glut.cloud.materialManage.service.IMaterialsManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 材料管理表 前端控制器
 *
 * @author sanminzhi
 * @since 2024-07-19 11:26
 */
@RestController
@Slf4j
public class MaterialsManageController {

    private final IMaterialsManageService iMaterialsManageService;

    @Autowired
    public MaterialsManageController(IMaterialsManageService iMaterialsManageService){
        this.iMaterialsManageService = iMaterialsManageService;
    }

    @Autowired
    private MaterialChangeMapper materialChangeMapper;

    @GetMapping("/mm/getData")
    public List<MaterialsManage> getMaterialsManage(){
        return iMaterialsManageService.getMaterialsManage();
    }

    @PostMapping("/mm/mid")
    public List<MaterialsManage> findMaterials(@RequestBody MaterialsManage materialName){
        log.info("参数值{}",materialName);
        return iMaterialsManageService.findMaterials(materialName.getMaterialName());
    }

    @Autowired
    IMaterialsManageService materialsManageService;

    @PostMapping("/materials-manage/save")
    public boolean save(@RequestBody MaterialsManage materialsManage) {
        return materialsManageService.save(materialsManage);
    }

    @GetMapping("/materials-manage/selectAll")
    public List<MaterialsManage> selectAll() {
        return materialsManageService.selectAll();
    }

    @GetMapping("/materials-manage/selectPage")
    public Page<MaterialsManage> selectPage(
            @RequestParam int page,
            @RequestParam int size,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String query) {
        return materialsManageService.selectPage(page, size, category, query);
    }

    @GetMapping("/materials-manage/lowStock")
    public List<MaterialsManage> getLowStockMaterials() {
        return materialsManageService.getLowStockMaterials(10);
    }

    @PostMapping("/materials-manage/deduct")
    public boolean deductMaterial(@RequestParam Integer materialId, @RequestParam int quantity) {
        return iMaterialsManageService.deductMaterial(materialId, quantity);
    }

    @GetMapping("/materials-manage/change")
    public Page<MaterialChange> getMaterialChange(
            @RequestParam int page,
            @RequestParam int size) {
        return materialsManageService.getMaterialChange(page, size);
    }

    /**
     * 根据物料id查找出来，然后将物料取出。
     * @param materialId
     * @param quantity
     * @return
     */
    @PostMapping("upMaterialsManage/{materialId}")
    public MaterialsManage upMaterialsManage(@PathVariable Integer materialId, @RequestBody Integer quantity) {
        LambdaQueryWrapper<MaterialsManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialsManage::getMaterialId, materialId);

        // 查询符合条件的物料（只会有一条记录）
        MaterialsManage materialsManage = iMaterialsManageService.getOne(queryWrapper);

        // 判断是否查询到记录
        if (materialsManage == null) {
            throw new RuntimeException("Material not found for ID: " + materialId);
        }

        // 检查库存是否足够
        if (materialsManage.getQuantity() < quantity) {
            throw new RuntimeException("Insufficient stock for material ID: " + materialId);
        }

        // 减去库存
        materialsManage.setQuantity(materialsManage.getQuantity() - quantity);

        // 更新数据库中的记录
        iMaterialsManageService.updateById(materialsManage);

        // 创建并保存物料变动记录
        MaterialChange materialChange = new MaterialChange();
        materialChange.setMaterialId(materialId);
        materialChange.setMaterialName(materialsManage.getMaterialName()); // 确保 MaterialsManage 有 materialName 属性
        materialChange.setChangeQuantity(-quantity); // 取出物料，变动量为负
        materialChange.setChangeTime(new Date());
        materialChange.setChangeType("减少"); // 变动类型设置为 "减少"

        materialChangeMapper.insert(materialChange);

        return materialsManage;
    }


}
